home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / filesy~1 / mfsdefrg.zoo / minix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-18  |  5.9 KB  |  188 lines

  1. /*
  2.  * minix.c - minix-specific functions and data for the Linux file system 
  3.  * degragmenter. 
  4.  * minix.c,v 1.1 1992/12/23 03:43:22 linux Exp
  5.  *
  6.  * This file is responsible for managing those data structures dependent
  7.  * on the minix.  Specifically, it
  8.  * + allocates space for the various disk maps stored in memory; 
  9.  * + reads and writes superblock information; 
  10.  * + reads the used-data-zones and used-inodes maps into memory
  11.  *   (inode_map and d2n/n2d_map respectively); and, 
  12.  * + once the defragmentation is complete, rewrites the new free-space
  13.  *   map to disk.
  14.  *
  15.  * Copyright (C) 1992 Stephen Tweedie (sct@dcs.ed.ac.uk)
  16.  *
  17.  * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
  18.  *
  19.  * Copyright (C) 1991 Linus Torvalds (torvalds@kruuna.helsinki.fi)
  20.  * 
  21.  * This file may be redistributed under the terms of the GNU General
  22.  * Public License.
  23.  *
  24.  */
  25.  
  26. /* Modified for Minixfs/MiNT by S N Henson 1993 */
  27.  
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <memory.h>
  32.  
  33. #include "defrag.h"
  34.  
  35. char * program_name = "defrag";
  36.  
  37. /* A few static variables and functions, needed only within minix.c
  38.    for handling minix-specific data structures (like the free zone
  39.    map) */
  40. static char *zone_map = NULL;
  41.  
  42. #define bm_zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
  43. #define bm_mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
  44. #define bm_unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
  45.  
  46.  
  47. /* Read the superblock and inode table and reserve space for the
  48.    remaining disk map tables */
  49. void read_tables (void)
  50. {
  51.     if (debug)
  52.         printf ("DEBUG: read_tables()\n");
  53.     if (BLOCK_SIZE != lseek (IN, BLOCK_SIZE, SEEK_SET))
  54.         die ("seek failed");
  55.     if (BLOCK_SIZE != read (IN, super_block_buffer, BLOCK_SIZE))
  56.         die ("unable to read super-block");
  57.     if (MAGIC != MINIX_SUPER_MAGIC)
  58.         die ("bad magic number in super-block");
  59.     if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
  60.         die("Only 1k blocks/zones supported");
  61.     if (!IMAPS || IMAPS > MINIX_I_MAP_SLOTS)
  62.         die("bad s_imap_blocks field in super-block");
  63.     if (!ZMAPS || ZMAPS > MINIX_Z_MAP_SLOTS)
  64.         die("bad s_zmap_blocks field in super-block");
  65.  
  66.     inode_map = malloc (IMAPS*BLOCK_SIZE);
  67.     if (!inode_map)
  68.         die ("Unable to allocate inodes bitmap\n");
  69.     if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE))
  70.         die("Unable to read inode map");
  71.     if(set_size(ZONES))
  72.         die("Can't access partition");
  73.  
  74.  
  75.     d2n_map = malloc ((ZONES - FIRSTZONE) * sizeof (*d2n_map));
  76.     n2d_map = malloc ((ZONES - FIRSTZONE) * sizeof (*n2d_map));
  77.     if (!n2d_map || !d2n_map)
  78.         die ("Unable to allocate zone maps\n");
  79.  
  80.     zone_map = malloc (ZMAPS*BLOCK_SIZE);
  81.     if (!zone_map)
  82.         die ("Unable to allocate zone bitmap\n");
  83.     if (ZMAPS*BLOCK_SIZE != read(IN,zone_map,ZMAPS*BLOCK_SIZE))
  84.         die("Unable to read zone map");
  85.  
  86.     inode_buffer = malloc (INODE_BUFFER_SIZE);
  87.     if (!inode_buffer)
  88.         die ("Unable to allocate buffer for inodes");
  89.     if (INODE_BUFFER_SIZE != read (IN, inode_buffer, INODE_BUFFER_SIZE))
  90.         die ("Unable to read inodes");
  91.  
  92.     inode_average_map = malloc (INODES * sizeof(*inode_average_map));
  93.     if (!inode_average_map)
  94.         die ("Unable to allocate buffer for inode averages");
  95.     memset (inode_average_map, 0, (INODES * sizeof(*inode_average_map)));
  96.  
  97.     inode_order_map = malloc (INODES * sizeof(*inode_order_map));
  98.     if (!inode_order_map)
  99.         die ("Unable to allocate buffer for inode order");
  100.     memset (inode_order_map, 0, (INODES * sizeof(*inode_order_map)));
  101.  
  102.     if (NORM_FIRSTZONE != FIRSTZONE)
  103.         printf ("Warning: Firstzone != Norm_firstzone\n");
  104.     first_zone = FIRSTZONE;
  105.     zones = ZONES;
  106.     block_size = BLOCK_SIZE;
  107.     
  108.     if (show)
  109.     {
  110.         printf ("%d inodes\n",INODES);
  111.         printf ("%d blocks\n",ZONES);
  112.         printf ("Firstdatazone=%d (%d)\n",FIRSTZONE,NORM_FIRSTZONE);
  113.         printf ("Zonesize=%d\n",BLOCK_SIZE<<ZONESIZE);
  114.         printf ("Maxsize=%d\n\n",MAXSIZE);
  115.     }
  116. }
  117.  
  118. /* Write the superblock, free zone/inode maps and inode table to disk */
  119. void write_tables (void)
  120. {
  121.     if (debug)
  122.         printf ("DEBUG: write_tables()\n");
  123.     if (BLOCK_SIZE != lseek (IN, BLOCK_SIZE, SEEK_SET))
  124.         die ("seek failed in write_tables");
  125.     if (BLOCK_SIZE != write (IN, super_block_buffer, BLOCK_SIZE))
  126.         die ("unable to write super-block");
  127.     if (IMAPS*BLOCK_SIZE != write(IN,inode_map,IMAPS*BLOCK_SIZE))
  128.         die("Unable to write inode map");
  129.     if (ZMAPS*BLOCK_SIZE != write(IN,zone_map,ZMAPS*BLOCK_SIZE))
  130.         die("Unable to write zone map");
  131.     if (INODE_BUFFER_SIZE != write(IN,inode_buffer,INODE_BUFFER_SIZE))
  132.         die("Unable to write inodes");
  133. }
  134.  
  135. /* Recalculate the free zone map.  The defragmentation procedure will
  136.    migrate all free blocks to the end of the disk partition, and so
  137.    after defragmentation the free space map must be updated to reflect
  138.    this. Free zones are determined by n2d_map, and the macro
  139.    zone_in_use(n) is defined in defrag.h for this purpose. The minix
  140.    fs stores the free zone map as a bitmap, so this bitmap must now be
  141.    recreated from the n2d_map. */
  142. void salvage_free_zones (void)
  143. {
  144.     Block i;
  145.     if (debug)
  146.         printf ("DEBUG: salvage_zone_freelist()\n");
  147.     memset (zone_map, -1, ZMAPS*BLOCK_SIZE);
  148.     for (i=FIRSTZONE; i<ZONES; i++)
  149.     {
  150.         if (!zone_in_use(i))
  151.             bm_unmark_zone(i);
  152.     }
  153. }
  154.  
  155.  
  156. /* Read the map of used/unused data zones on disk.  The extfs stores a
  157.    linked list of unused blocks.
  158.    The map is held jointly in d2n_map and n2d_map, described in
  159.    defrag.h.  These are initialised to the identity map (d2n(i) = n2d(i)
  160.    = i), and then the free zone bitmap is scanned, and all unused zones
  161.    are marked as zero in both d2n_map and n2d_map. */
  162. void init_zone_maps (void)
  163. {
  164.     Block i;
  165.  
  166.     if (debug)
  167.         printf ("DEBUG: init_zone_maps()\n");
  168.     for (i = FIRSTZONE; i < ZONES; i++)
  169.     {
  170.         if (bm_zone_in_use(i))
  171.         {
  172.             d2n(i) = i;
  173.             n2d(i) = i;
  174.         }
  175.         else
  176.         {
  177.             d2n(i) = 0;
  178.             n2d(i) = 0;
  179.         }
  180.     }
  181. }
  182.  
  183. /* Read in the map of used/unused inodes.  For the minix-fs, we don't
  184.    need to anything since the bitmap is read in by read_tables(). */
  185. void init_inode_bitmap (void)
  186. {
  187. }
  188.